package com.example.shiwuyouproject.net.http

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Looper
import android.util.Log
import android.util.TimeUtils
import android.widget.Toast
import com.example.shiwuyouproject.ui.pup.AlbumPopupView
import com.example.shiwuyouproject.utils.Auth
import com.example.shiwuyouproject.utils.GlideEngine
import com.example.shiwuyouproject.utils.GlideUtils
import com.example.shiwuyouproject.utils.MyTimeUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.lxj.xpopup.XPopup
import com.qiniu.android.common.FixedZone
import com.qiniu.android.storage.Configuration
import com.qiniu.android.storage.UploadManager
import com.yechaoa.yutilskt.ToastUtil
import java.text.SimpleDateFormat
import java.util.*

object QiNiuUpImageUtils {
    var accessKey = "5Mq4K0HoqmBl_Clmc4x358zCHC2Zbwg1pltAFBOG"
    var secretKey = "nQbAllsv15ly9yMf05_jil7exdCHsXS1k4V7WR5d"
    var bucket = "uswy"
    var config: Configuration = Configuration.Builder()
        .zone(FixedZone.zone2)
        .connectTimeout(10)           // 链接超时。默认10秒
        .useHttps(true)               // 是否使用https上传域名
        .responseTimeout(60)          // 服务器响应超时。默认60秒
        .recorder(null)           // recorder分片上传时，已上传片记录器。默认null
        .build()
    private var upLoadManager: UploadManager? = null
    private var upToken: String? = null

    /**
     * 上传图片到七牛云
     */
    @SuppressLint("SimpleDateFormat")
    fun postUpQiNiuFile(data: Intent?, onbackImage:onBackImageStatus) {
        // 图片选择结果回调
        val selectList = PictureSelector.obtainMultipleResult(data)
        upLoadManager = UploadManager(config)
        val auth: Auth = Auth.create(accessKey, secretKey)
        upToken = auth.uploadToken(bucket)
        val sdf = SimpleDateFormat("yyyyMMddHHmmss")

        val key1 = "images/android/"+MyTimeUtils.getYerMothDay()+"/"+"icon_" + sdf.format(Date())
        upLoadManager?.put(
            selectList[0].cutPath, key1, upToken,
            { key, info, res -> //res包含hash、key等信息，具体字段取决于上传策略的设置
                if (info.isOK) {
                    onbackImage.onSuccess( key)
                } else {
                    onbackImage.onFail()
                    Looper.loop()
                }
            }, null
        )
    }
    interface  onBackImageStatus{
        fun onSuccess(key:String)
        fun onFail()
    }


    /**
     * 打开相册
     */
    fun openCard(activity: Activity) {
        XPopup.Builder(activity)
            .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
            .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
            .asCustom(AlbumPopupView(activity, object : AlbumPopupView.OnBackItem {
                override fun onCamera() {
                    PictureSelector.create(activity)
                        .openCamera(PictureMimeType.ofImage())
                        .isEnableCrop(true)// 是否裁剪
                        .withAspectRatio(4, 4)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .cutOutQuality(90)// 裁剪输出质量 默认100
                        .minimumCompressSize(100)// 小于多少kb的图片不压缩
                        .forResult(PictureConfig.REQUEST_CAMERA)
                }

                override fun onXiangCe() {
                    PictureSelector.create(activity)
                        .openGallery(PictureMimeType.ofImage())
                        .isEnableCrop(true)// 是否裁剪
                        .maxSelectNum(1)//最大选择数量,默认9张
                        .withAspectRatio(4, 4)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .cutOutQuality(90)// 裁剪输出质量 默认100
                        .minimumCompressSize(100)// 小于多少kb的图片不压缩
                        .imageEngine(GlideEngine.createGlideEngine())
                        .isCompress(true)//是否压缩
                        .forResult(PictureConfig.REQUEST_CAMERA)
                }
            }))
            .show()


    }
}